package com.zzzzzz.oms.tempShipment;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zzzzzz.oms.legs.Legs;
import com.zzzzzz.oms.legs.LegsDao;
import com.zzzzzz.oms.shipment.Shipment;
import com.zzzzzz.oms.shipment.ShipmentService;
import com.zzzzzz.oms.vehicle.VehicleDao;
import com.zzzzzz.plugins.shiro.I;
import com.zzzzzz.plugins.shiro.ShiroUtils;

@Service
public class TempShipmentService {
	@Resource
	public TempShipmentDao tempShipmentDao;
	
	@Resource
	public Validator validator;
	
	@Resource
	public LegsDao legsDao;
	
	@Resource
	public ShipmentService shipmentService;
	
	@Resource
	public VehicleDao vehicleDao;
	
	@Transactional
	public  synchronized TempShipment  save(TempShipment tempShipment ,String vocationType,List<Long> ids,Long tempshipmentId,I i) {
		tempShipment.setVocationType(vocationType);
		tempShipment.setPlatformId(i.getPlatformId());
		tempShipment.setAddBy(i.getId());
		tempShipment.setAddDt(new Date());
		Long id = tempShipmentDao.add(tempShipment);
		legsDao.updTempshipmenByIds(id, ids, i);
		tempShipment = build(id, i);
		tempShipment.setId(id);
		tempShipmentDao.updById(tempShipment);
		TempShipment tempShipment1 =udpTemp(tempshipmentId,i);
		return tempShipment1;
	}

	//更新tempshipment的值
	public TempShipment udpTemp(Long tempshipmentId,I i){
		TempShipment tempShipment = build(tempshipmentId, i);
		tempShipment.setId(tempshipmentId);
		tempShipmentDao.updById(tempShipment);
		return tempShipment;
	}

	//取消配载
	public synchronized void cancel(List<Long> ids,I i){
		List<TempShipment> list=tempShipmentDao.findByIds(ids);
		TempShipment tempShipment=tempShipmentDao.findIdByCd("未配载", list.get(0).getVocationType(), i);
		Long tempshipmentId = tempShipment.getId();
		legsDao.updStById(ids, tempshipmentId, i);
		tempShipment = build(tempshipmentId, i);
		tempShipment.setId(tempshipmentId);
		tempShipmentDao.updById(tempShipment);
		//删除temp表中的记录
		tempShipmentDao.delById(ids);
		
	}
	
	//确认配载
	public void saveShipment(List<Long> ids,I i){
		List<TempShipment> list=tempShipmentDao.findByIds(ids);
		for(TempShipment tempShipment:list){
			legsDao.updByTempShipmentId(tempShipment.getId(), tempShipment.getConstractorId(), tempShipment.getShipment_method(), "shipmentEd", ShiroUtils.findUser());
			Shipment shipment=new Shipment();
			shipment.setTempShipmentId(tempShipment.getId());
			shipment.setConstractorId(tempShipment.getConstractorId());
			shipment.setVehicleId(tempShipment.getVehicleId());
			shipment.setExpense(tempShipment.getExpense());
			shipment.setExpectMoney(tempShipment.getExpectMoney());
			shipment.setShipment_method(tempShipment.getShipment_method());
			shipment.setQuantity(tempShipment.getQuantity());
			shipment.setWeight(tempShipment.getWeight());
			shipment.setVolume(tempShipment.getVolume());
			shipment.setPalletsum(tempShipment.getPalletsum());
			shipment.setDriverId(tempShipment.getDriverId());
			shipment.setPoints(tempShipment.getCount());
			shipment.setVocationType(tempShipment.getVocationType());
			shipment.setPlanatime(tempShipment.getPlanaDt());
			shipment.setPlanltime(tempShipment.getPlanlDt());
			Long id=shipmentService.save(shipment, i);
			legsDao.updShipmentId(tempShipment.getId(), id,ShiroUtils.findUser());
		}
		//删除表中的记录
		tempShipmentDao.delById(ids);
	}
	
	//根据波次自动配载
	public void findGroupByWaveIds(Long tempShipmentId,I i){
		List<Legs> list=legsDao.findGroupByWaveIds(tempShipmentId);
		TempShipment tempShipment;
		for(Legs legs:list){
			tempShipment=new TempShipment();
			tempShipment.setCode(legs.getWaveCd());
			tempShipment.setQuantity(legs.getQuantity());
			tempShipment.setWeight(legs.getWeight());
			tempShipment.setVolume(legs.getVolume());
			tempShipment.setPalletsum(legs.getPalletsum());
			tempShipment.setCount(legs.getCount());
			tempShipment.setPoints(legs.getPoints());
			tempShipment.setVocationType(legs.getVocationType());
			tempShipment.setExpense(0.0);
			tempShipment.setExpectMoney(0.0);
			tempShipment.setAddDt(new Date());
			tempShipment.setAddBy(i.getId());
			tempShipment.setPlatformId(i.getPlatformId());
			Long id=tempShipmentDao.add(tempShipment);
			legsDao.updByWaveId(legs.getWaveId(), id,tempShipmentId, i);
		}
		tempShipment=udpTemp(tempShipmentId, i);
	}
	
	//根据波次自动配车
	public void bczdpc(String vocationType,I i){
		List<TempShipment> list=tempShipmentDao.findByCode(vocationType, i);
		if(list.size()>0){
			for(TempShipment tempShipment:list){
					tempShipment.setExpense(0.0);
					tempShipment.setExpectMoney(0.0);
					tempShipmentDao.updStByIds(tempShipment, i);
				}
		}
	
	}
		
	//增加订单
	public void zjdd(Long temshipmentId,Long temshipmentId1,List<Long> legsIds,I i){			
		//temshipmentId是要增加的id，temshipmentId1是减少		
		legsDao.updTempshipmenByIds(temshipmentId, legsIds, i);
		//未配载
		TempShipment tempShipment = build(temshipmentId, i);
		tempShipment.setId(temshipmentId);
		tempShipmentDao.updById(tempShipment);
		//配载
		TempShipment tempShipment1 = build(temshipmentId1, i);			
		tempShipment1.setId(temshipmentId1);
		tempShipmentDao.updById(tempShipment1);
	}
		
	//取消高级调度里面的调度单
	public void qxdd(Long temshipmentId,Long temshipmentId1,List<Long> legsIds,I i){					
			//temshipmentId是要增加的id，temshipmentId1是未配载的
			legsDao.updTempshipmenByIds(temshipmentId1, legsIds, i);
			//未配载
			TempShipment tempShipment = build(temshipmentId, i);
			tempShipment.setId(temshipmentId);
			tempShipmentDao.updById(tempShipment);
			//配载
			TempShipment tempShipment1 = build(temshipmentId1, i);
			tempShipment1.setId(temshipmentId1);			
			tempShipmentDao.updById(tempShipment1);
	}
	
	//生成tempshipment
	public TempShipment build(Long tempshipmentId, I i){
		Legs legs=legsDao.findtempIds(tempshipmentId);
		TempShipment tempShipment=new TempShipment();
		if(legs.getQuantity() == null){
			legs.setQuantity(0.0);
		}
		if(legs.getWeight() == null){
			legs.setWeight(0.0);
		}
		if(legs.getVolume() == null){
			legs.setVolume(0.0);
		}
		if(legs.getPalletsum() == null){
			legs.setPalletsum(0.0);
		}
		tempShipment.setQuantity(legs.getQuantity());
		tempShipment.setWeight(legs.getWeight());
		tempShipment.setVolume(legs.getVolume());
		tempShipment.setPalletsum(legs.getPalletsum());
		tempShipment.setPoints(legs.getPoints());
		tempShipment.setCount(legs.getCount());
		tempShipment.setUpdBy(i.getId());
		tempShipment.setUpdDt(new Date());
		tempShipmentDao.updById(tempShipment);
		return tempShipment;
	}
		
	private static final Logger logger = LoggerFactory.getLogger(TempShipment.class);
}
